package com.chaokang.service;

import com.chaokang.dao.CommonUserEntityMapper;
import com.chaokang.dao.GroupEntityMapper;
import com.chaokang.dao.GroupMemberEntityMapper;
import com.chaokang.dao.entity.CommonUserEntity;
import com.chaokang.dao.entity.GroupEntity;
import com.chaokang.dao.entity.GroupMemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class GroupMemberService {
    @Autowired
    GroupMemberEntityMapper groupMemberEntityMapper;
    @Autowired
    GroupEntityMapper groupEntityMapper;


    @Autowired
    CommonUserEntityMapper commonUserEntityMapper;
    public int insertGroupMember(GroupMemberEntity groupMemberEntity){
        List<GroupMemberEntity> groupMemberIds = groupMemberEntityMapper.getGroupMemberByGroupId(groupMemberEntity);

        for(GroupMemberEntity g:groupMemberIds){
            if(g.getMemberId().equals(groupMemberEntity.getMemberId()))
                return 0;
        }
        groupEntityMapper.addOneMember(groupMemberEntity.getGroupId());
        return groupMemberEntityMapper.insertGroupMember(groupMemberEntity);
    }

    public List<GroupEntity> getGroupByUserId(GroupEntity groupEntity){

        return groupEntityMapper.getGroupByUserId(groupEntity);
    }
    public List<CommonUserEntity> getGroupMemberByGroupId(GroupMemberEntity groupMemberEntity){
        List<GroupMemberEntity> groupMemberIds = groupMemberEntityMapper.getGroupMemberByGroupId(groupMemberEntity);
        LinkedList<CommonUserEntity> userList =new LinkedList<>();
        for(GroupMemberEntity g:groupMemberIds){
            userList.add(commonUserEntityMapper.getUserById(g.getUserId()));
        }
        return userList;

    }
    public int deleteGroupMember(GroupMemberEntity groupMemberEntity){
        groupEntityMapper.deleteOneMember(groupMemberEntity.getGroupId());

        return groupMemberEntityMapper.deleteGroupMember(groupMemberEntity);
    }

    public int addGroup(GroupEntity groupEntity){
        return groupEntityMapper.addGroup(groupEntity);
    }


}
